package com.technology.education.ui.market.popularize

import android.widget.ImageView
import com.angcyo.dsladapter.DslAdapterItem
import com.angcyo.dsladapter.dslItem
import com.angcyo.dsladapter.toDslItemList
import com.angcyo.dsladapter.updateOrInsertHeaderItem
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.model.InviteChargeRecord
import com.technology.education.ext.header
import com.technology.education.ext.setPageItems
import com.technology.education.ext.toast
import me.hgj.jetpackmvvm.ext.parseState

/**
 * 学生充值记录
 */
class PopularizeDetailFragment : BaseRecyclerTopBarFragment<PopularizeDetailViewModel>() {
    override fun initViews() {
        setTitle("学生充值记录")
        mViewModel.info = arguments?.getParcelable("info")
    }

    override fun lazyLoadData() {
        loading()
        setUI()
    }

    private fun setUI() {
        renderAdapter {
            updateOrInsertHeaderItem<DslAdapterItem>("info", 0) {
                it.apply {
                    itemLayoutId = R.layout.item_popularize_detail_info
                    itemBindOverride = { itemHolder, _, _, _ ->
                        itemHolder.tv(R.id.tv_name)?.text = mViewModel.info?.name
                        itemHolder.tv(R.id.tv_info)?.text =
                            "充值总金额: ${mViewModel.info?.totalChargeAmount}"
                        itemHolder.v<ImageView>(R.id.iv_header)?.apply {
                            header(mViewModel.info?.avatar, this)
                        }
                    }
                }
            }
        }
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { it ->
                hasMore(it.total)
                renderAdapter {
                    setPageItems(it.list.toDslItemList(R.layout.item_score_record) {
                        itemBindOverride = { itemHolder, _, _, _ ->
                            itemData?.let { it ->
                                if (it is InviteChargeRecord) {
                                    itemHolder.tv(R.id.tv_title)?.text = it.placeTime
                                    itemHolder.gone(R.id.tv_date)
                                    itemHolder.tv(R.id.tv_score)?.apply {
                                        text = "充值¥${it.amount}"
                                        textSize = 14f
                                    }
                                }
                            }
                        }
                    }, mViewModel.pageIndex)
                }
            }, onError = { err -> toast(err.errorMsg) })
        }
    }
}